package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.vo.PageVo;
import com.dd.cloud.user.entity.order.OrderFlow;
import com.dd.cloud.user.entity.order.OrderPay;
import com.dd.cloud.user.req.consult.ImUpdateUserReq;
import com.dd.cloud.user.req.order.*;
import com.dd.cloud.user.res.consult.ConsultMedicalRecordDetailRes;
import com.dd.cloud.user.res.order.*;
import com.dd.cloud.user.service.consule.IConsultSubscribeService;
import com.dd.cloud.user.service.consule.ITencentImMsgService;
import com.dd.cloud.user.service.order.IOrderFlowService;
import com.dd.cloud.user.service.order.IOrderPayService;
import com.dd.cloud.user.service.paynotice.NoticeOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;

/**
 * 订单控制器
 *
 * @author zk
 */
@RestController
@RequestMapping("/order")
@Validated
@Slf4j
public class OrderController {
    @Autowired
    private IOrderPayService orderPayService;
    @Autowired
    private IOrderFlowService orderFlowService;
    @Autowired
    private NoticeOrderService noticeOrderService;

    @Autowired
    private IConsultSubscribeService consultSubscribeService;


    /**
     * 支付的成功回调 新接口
     *
     * @return 返回处理结果
     */
    @RequestMapping(value = "/pay/notice/{redisKey}", produces = MediaType.APPLICATION_XML_VALUE)
    public ResponseEntity wxPayNotify(HttpServletRequest request, @PathVariable("redisKey") String redisKey) {
        ResponseEntity entity = noticeOrderService.doNotice(request, redisKey);
        return entity;
    }


    /**
     * 退款的成功回调 新接口
     *
     * @return 返回处理结果
     */
    @RequestMapping(value = "/pay/refund/{redisKey}", produces = MediaType.APPLICATION_XML_VALUE)
    public ResponseEntity refundNotify(HttpServletRequest request, @PathVariable("redisKey") String redisKey) throws IOException {
        ResponseEntity entity = noticeOrderService.refundNotice(request, redisKey);
        return entity;
    }

    /**
     * 订单退款
     *
     * @param orderNo 订单编号
     */
    @PostMapping(value = "/order/refund")
    public ResponseEntity<Void> refundNotify(@NotBlank(message = "订单编号不能为空") String orderNo) {
        orderPayService.refund(orderNo, 0);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询是否退款成功
     */
    @GetMapping(value = "/order/refund/success")
    public ResponseEntity<String> refundIsSuccess(@NotBlank(message = "订单编号不能为空") String orderNo) {
        String res = orderPayService.refundIsSuccess(orderNo);
        return ResponseEntity.ok(res);
    }

    /**
     * 未支付方订单重新支付
     *
     * @param req 订单编号
     */
    @PostMapping(value = "/order/payagain")
    public ResponseEntity<PayOrderResponse<?>> orderPayagain(HttpServletRequest request, @Valid PayUnOrderReq req) {
        PayOrderResponse<?> res = orderPayService.payUnPayOrder(request, req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询订单情况
     *
     * @param orderNo
     * @return
     */
    @GetMapping(value = "/query")
    public ResponseEntity<OrderPay> getOrderPay(@NotBlank(message = "订单编号不能为空") String orderNo) {
        OrderPay order = orderPayService.getOrderPay(orderNo);
        return ResponseEntity.ok(order);
    }

    /**
     * 取消订单
     *
     * @param orderNo
     */
    @PostMapping(value = "/cancel")
    public ResponseEntity<Void> cancelOrder(@NotBlank(message = "订单编号不能为空") String orderNo) {
        orderPayService.cancelOrder(orderNo, LoginInfo.UserType.ADMIN);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询流水情况
     *
     * @return 返回处理结果
     */
    @GetMapping(value = "/flow/query")
    public ResponseEntity<OrderFlow> flow(@NotBlank(message = "流水编号不能为空") String flowNo) {
        OrderFlow flow = orderFlowService.getOrderFlow(flowNo);
        return ResponseEntity.ok(flow);
    }

    /**
     * 查询处方订单列表/患者治疗记录
     *
     * @param req
     * @return
     */
    @GetMapping(value = "/pres/page")
    public ResponseEntity<IPage<OrderPresPageRes>> queryOrderPresPage(@Valid OrderPresPageReq req) {
        IPage<OrderPresPageRes> res = orderPayService.queryOrderPresPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询处方订单列表 运营后台
     *
     * @param req
     * @return
     */
    @GetMapping(value = "/pres/admin/page")
    public ResponseEntity<IPage<OrderPresAdminPageRes>> queryOrderPresAdminPage(@Valid OrderPresAdminPageReq req) {
        IPage<OrderPresAdminPageRes> res = orderPayService.queryOrderPresAdminPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询处方订单列表 运营后台 导出excel
     *
     * @param response
     * @param req
     */
    @GetMapping(value = "/pres/admin/export")
    public ResponseEntity<Void> exportOrderPresAdminExcel(HttpServletResponse response, OrderPresAdminPageReq req) {
        orderPayService.exportOrderPresAdminExcel(response, req);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询处方订单列表 药房后台
     *
     * @param req
     * @return
     */
    @GetMapping(value = "/pres/pharmacy/page")
    public ResponseEntity<IPage<OrderPresPharmacyPageRes>> queryOrderPresPharmacyPage(@Valid OrderPresPharmacyPageReq req) {
        IPage<OrderPresPharmacyPageRes> res = orderPayService.queryOrderPresPharmacyPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询处方交易明细列表 药房后台
     *
     * @param req
     * @return
     */
    @GetMapping(value = "/pres/detail/page")
    public ResponseEntity<PageVo<OrderPresDetailListRes>> queryOrderPresDetailListPage(@Valid OrderPresDetailListReq req) {
        PageVo<OrderPresDetailListRes> res = orderPayService.queryOrderPresDetailListPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 导出处方交易明细列表 药房后台
     *
     * @param req
     * @param response
     */
    @GetMapping(value = "/pres/detail/export")
    public ResponseEntity<Void> exportOrderPresDetailList(OrderPresDetailListReq req, HttpServletResponse response) {
        orderPayService.exportOrderPresDetailList(req, response);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询处方收入统计列表 药房后台
     *
     * @param req
     * @return
     */
    @GetMapping(value = "/pres/income/statistics/page")
    public ResponseEntity<PageVo<OrderPresIncomeStatisticsListRes>> queryOrderPresIncomeStatisticsList(@Valid OrderPresIncomeStatisticsListReq req) {
        PageVo<OrderPresIncomeStatisticsListRes> res = orderPayService.queryOrderPresIncomeStatisticsList(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 导出处方收入统计列表 药房后台
     *
     * @param req
     * @param response
     */
    @GetMapping(value = "/pres/income/statistics/export")
    public ResponseEntity<Void> exportOrderPresIncomeStatisticsList(OrderPresIncomeStatisticsListReq req, HttpServletResponse response) {
        orderPayService.exportOrderPresIncomeStatisticsList(req, response);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询处方订单收益列表
     *
     * @param req
     * @return
     */
    @GetMapping(value = "/profit/page")
    public ResponseEntity<PageVo<OrderProfitPageRes>> queryOrderProfitPage(@Valid OrderProfitPageReq req) {
        PageVo<OrderProfitPageRes> res = orderPayService.queryOrderProfitPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询订单详情
     *
     * @param orderNo
     * @return
     */
    @GetMapping(value = "/pres/detail")
    public ResponseEntity<OrderPresDetailRes> queryOrderPresDetail(@NotEmpty(message = "订单号不能为空") String orderNo) {
        OrderPresDetailRes res = orderPayService.queryOrderPresDetail(orderNo);
        return ResponseEntity.ok(res);
    }

    /**
     * 结算订单
     *
     * @param req
     */
    @PostMapping(value = "/settlement")
    public ResponseEntity<Void> settlementOrder(@Valid @RequestBody OrderSettlementReq req) {
        orderPayService.settlementOrder(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 导出处方订单收益列表excel
     *
     * @param response
     * @param req
     * @return
     */
    @GetMapping(value = "/profit/excel/export")
    public ResponseEntity<Void> exportOrderProfitExcel(HttpServletResponse response, OrderProfitPageReq req) {
        orderPayService.exportOrderProfitExcel(response, req);
        return ResponseEntity.ok().build();
    }

    /**
     * 问诊处方订单列表
     */
    @GetMapping("/inquiry/pres/page")
    public ResponseEntity<IPage<InquiryPresRes>> selectInquiryPres(@Valid InquiryPresReq inquiryPresReq) {
        IPage<InquiryPresRes> res = orderPayService.selectInquiryPres(inquiryPresReq);
        return ResponseEntity.ok(res);
    }

    /**
     * 问诊订单列表
     */
    @GetMapping("/inquiey/order/page")
    public ResponseEntity<IPage<InquireOrderRes>> selectInquiryOrder(@Valid InquiryOrderReq inquiryOrderReq) {
        IPage<InquireOrderRes> res = orderPayService.selectInquiryOrder(inquiryOrderReq);
        return ResponseEntity.ok(res);
    }

    /**
     * 查看患者电子病例
     */
    @GetMapping("/medical/record/detail")
    public ResponseEntity<ConsultMedicalRecordDetailRes> queryMedicalRecord(@RequestParam("consultId") Integer consultId) {
        ConsultMedicalRecordDetailRes medicalDetailRes = consultSubscribeService.queryMedicalRecord(consultId);
        return ResponseEntity.ok(medicalDetailRes);
    }

    @Autowired
    private ITencentImMsgService tencentImMsgService;

    /**
     * 测试
     */
    @GetMapping("/test")
    public ResponseEntity<Void> test(@RequestParam("userId") Integer userId) {
        ImUpdateUserReq imUpdateUserReq = new ImUpdateUserReq();
        imUpdateUserReq.setUserId(userId);
        imUpdateUserReq.setUserName("666");
        imUpdateUserReq.setUserType(1);
        imUpdateUserReq.setFaceUrl("https://zk-med-test.obs.cn-south-1.myhuaweicloud.com/dadao/65b08dad-83d1-4e46-8bb5-dbb222cffccb");
        tencentImMsgService.imUpdateImUser(imUpdateUserReq);
        return ResponseEntity.ok().build();
    }
}
